<?php

/**
 * @Author: user
 * @Date:   2019-03-05 10:48:44
 * @Last Modified by:   user
 * @Last Modified time: 2019-03-05 11:04:19
 */
/**
 * 公司管理api
 */
namespace app\api\controller;
use think\Controller;
use think\Db;
use app\api\model\ApiCompanyModel;

/**
 * 公司管理api控制器类
 */
class ApiCompanyController extends Controller
{
	//模型
	protected $companyModel=null;
	/**
	 * 获取公司行业分类列表信息
	 * @return  json  分类信息列表
	 */
	public function getCompanyTypeList()
	{
		$companyModel=new ApiCompanyModel();
		$result=$companyModel->getCompanyTypeList();
		return  cmf_replace_content_file_url(htmlspecialchars_decode(json_encode($result,JSON_UNESCAPED_UNICODE)));
	}

	public function path_auth()
	{
		if (!isset($_SERVER['HTTP_KEY']) || !isset($_SERVER['HTTP_COMPANYID']) || !isset($_SERVER['HTTP_APPSECRET'])) {
			header('HTTP/1.0 401 Unauthorized');
			exit;
		}
		$key = $_SERVER['HTTP_KEY'];
		$companyid = $_SERVER['HTTP_COMPANYID'];
        $appsecret = $_SERVER['HTTP_APPSECRET'];
		$path_uri = $_SERVER['HTTP_X_ORIGINAL_URI'];
		//file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/aa.text',serialize($_SERVER));
		/*
		$key = '86793af7c3bc64fd197001a8be4c6f9a';
		$companyid = 8;
        $appsecret = '364b904d5838144941d56e593d929097';
		$path_uri = '/chat1/a.php';
		*/

		$primary_path = substr($path_uri,1,strpos($path_uri, '/',1)-1);
		
		$where['id'] = $companyid;
		$where[] = ['exp',"FIND_IN_SET('$primary_path',path)"]; 
		$find = db('company')->where($where)->find();
		//echo db('company')->getlastsql();
		if ($find) {

			$_appid = md5(md5($find['id'].$find['create_time']));
			$_key = md5($find['id'].$find['create_time']) ; 
		    $_appsecret = md5(md5($find['id'].$_appid.$_key.$find['create_time'])); 

			if ($_key == $key && $_appsecret == $appsecret ) {
				exit('ok');
			} else {
				header('HTTP/1.0 401 Unauthorized');
				exit;
			}

		} else {
			header('HTTP/1.0 401 Unauthorized');
			exit;
		}
		header('HTTP/1.0 401 Unauthorized');
		exit;
	}

	public function path_get()
	{
		/*
		$primary_path = 'chat';

		$key = '86793af7c3bc64fd197001a8be4c6f9a';
		$companyid = 8;
        $appsecret = '364b904d5838144941d56e593d929097';

		$where['id'] = $companyid;
		$where[] = ['exp',"FIND_IN_SET('$primary_path',path)"]; 
		$find = db('company')->where($where)->find();
		//echo db('company')->getlastsql();
		if ($find) {

			$_appid = md5(md5($find['id'].$find['create_time']));
			$_key = md5($find['id'].$find['create_time']) ; 
		    $_appsecret = md5(md5($find['id'].$_appid.$_key.$find['create_time'])); 

			if ($_key == $key && $_appsecret == $appsecret ) {
				exit('ok');
			} else {
				header('HTTP/1.0 401 Unauthorized');
			}

		} else {
			header('HTTP/1.0 401 Unauthorized');
		}
		*/
		/* echo '<pre>';
		$cc = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/test/aa.text');
		print_r(unserialize($cc));
		exit; */

		/* $key = $_SERVER['HTTP_KEY'];
		$companyid = $_SERVER['HTTP_COMPANYID'];
        $appsecret = $_SERVER['HTTP_APPSECRET'];
		$path_uri = $_SERVER['HTTP_X_ORIGINAL_URI'];
		file_put_contents($_SERVER['DOCUMENT_ROOT'].'/test/aa.text',serialize($_SERVER)); */
		
		$key = '86793af7c3bc64fd197001a8be4c6f9a';
		$companyid = 8;
        $appsecret = '364b904d5838144941d56e593d929097';
		$path_uri = '/chat/a.php';
		

		$primary_path = substr($path_uri,1,strpos($path_uri, '/',1)-1);
		
		$where['id'] = $companyid;
		$where[] = ['exp',"FIND_IN_SET('$primary_path',path)"]; 
		$find = db('company')->where($where)->find();
		echo db('company')->getlastsql();
		if ($find) {

			$_appid = md5(md5($find['id'].$find['create_time']));
			$_key = md5($find['id'].$find['create_time']) ; 
		    $_appsecret = md5(md5($find['id'].$_appid.$_key.$find['create_time'])); 

			if ($_key == $key && $_appsecret == $appsecret ) {
				exit('ok');
			} else {
				header('HTTP/1.0 401 Unauthorized');
				exit;
			}

		} else {
			header('HTTP/1.0 401 Unauthorized');
			exit;
		}
		header('HTTP/1.0 401 Unauthorized');
		exit;
	}
}
